package com.umfb.redis;

import com.umfb.config.zk.ZooKeeperPropertyPlaceholderConfigurer;
import com.umfb.utils.UmfbApplicationContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.annotation.Resource;

@SpringBootApplication
@EnableAutoConfiguration
public class DubboDemoRedisApplication extends WebMvcConfigurerAdapter {
	private static final Logger logger = LoggerFactory.getLogger(DubboDemoRedisApplication.class);
	public static void main(String[] args) {
		SpringApplication springApplication = new SpringApplication (DubboDemoRedisApplication.class);
		final ApplicationContext ac = springApplication.run(DubboDemoRedisApplication.class,args);
		UmfbApplicationContext.setApplicationContext(ac);
		logger.info("-------------------Dubbo Redis service started----------------------");
	}

	@Bean
	public UmfbApplicationContext initAppEnd(ApplicationContext ac) {
		UmfbApplicationContext.setApplicationContext(ac);

		return new UmfbApplicationContext();
	}

	private ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
	@Bean
	public PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {

		PropertySourcesPlaceholderConfigurer configurer = new ZooKeeperPropertyPlaceholderConfigurer();
		configurer.setIgnoreResourceNotFound(true);
		configurer.setLocations(resourcePatternResolver.getResource("classpath:application.properties"));

		return configurer;
	}

	@Override
	public void addCorsMappings(CorsRegistry registry) {

		registry.addMapping("/**").allowedOrigins("*");
	}
/*	@Resource
	public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory jedisConnectionFactory) {
		logger.info("================");
		RedisTemplate<String, Object> rt = new RedisTemplate<>();
		rt.setConnectionFactory(jedisConnectionFactory);
		rt.setKeySerializer(new StringRedisSerializer());

		return rt;
	}*/

}
